package MyCustomTable;

import MyUtil.DataBaseUtil;
import MyUtil.MyPanel;

import javax.swing.*;
import java.awt.*;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Salemen_addCustomer extends JFrame{
    // 数据库相关
    private ResultSet rs;
    private String sql1="insert into Customer values(?,?,?,?,?,?)";
    private String sql2="insert into Customer(Cid,Cname,Cphone,Cemail,Clevel) values(?,?,?,?,?)";
    private String sql3 = "select max(Cid) from Customer";
    private String cid = null;
    private int cid_int = -1;
    //建立二个控件

    private JPanel jpanel1 = new MyPanel("/picture/5.jpg");

    //空间二的内容

    //人名
    JLabel jc1  = new JLabel("顾客姓名");
    JTextField jt1 = new JTextField(10);
    //账号
    JLabel jc2 = new JLabel("手机");
    JTextField jt2 = new JTextField(10);
    //密码
    JLabel jc3 = new JLabel("邮箱");
    JTextField jt3 = new JTextField(10);
    //等级
    JLabel jc4 = new JLabel("等级");
    JTextField jt4 = new JTextField(11);
    //薪水
    JCheckBox jc5 = new JCheckBox("备注（不输入时,不开即可）");
    JTextField jt5 = new JTextField(10);
    //按钮
    JButton jb1 = new JButton("添加");

    public Salemen_addCustomer() throws SQLException {
        //设置总JFrame属性
        super("添加顾客信息");
        setBounds(400,200,800,700);
        setContentPane(jpanel1);
        this.setVisible(true);
        //设置控件二
        jpanel1.add(jc1);
        jpanel1.add(jt1);
        jpanel1.add(jc2);
        jpanel1.add(jt2);
        jpanel1.add(jc3);
        jpanel1.add(jt3);
        jpanel1.add(jc4);
        jpanel1.add(jt4);
        jpanel1.add(jc5);
        jpanel1.add(jt5);
        jpanel1.add(jb1);
        jt1.setText("");
        jt2.setText("");
        jt3.setText("");
        jt4.setText("");
        jt5.setText("");
        jpanel1.setLayout(new CustomLayout());
        cid = getFirstCustomerEid();
        jb1.addActionListener((e)->{
            if(jc5.isSelected()) {
                if ((jt1.getText()).equals("")){
                    JOptionPane.showMessageDialog(this,"有项目缺少数据!");
                }else if ((jt2.getText()).equals("")){
                    JOptionPane.showMessageDialog(this,"有项目输入数据!");
                }else if (jt3.getText().equals("")){
                    JOptionPane.showMessageDialog(this,"有项目缺少数据!");;
                }else if ((jt4.getText()).equals("")||(Integer.parseInt(jt4.getText()))<0){
                    JOptionPane.showMessageDialog(this,"有项目缺少数据!");
                }else if ((jt5.getText()).equals("")){
                    JOptionPane.showMessageDialog(this,"有项目缺少数据!");
                }else{
                    Object[] objects = new Object[6];
                    objects[0] = cid;
                    objects[1] = jt1.getText();
                    objects[2] = jt2.getText();
                    objects[3] = jt3.getText();
                    objects[4] = Integer.parseInt(jt4.getText());
                    objects[5] = jt5.getText();
                    try {
                        DataBaseUtil.changeResultSet(sql1,objects);
                    } catch (SQLException ex) {
                        ex.printStackTrace();
                    }
                    cid = getNextCustomerEid();
                }
            }
            else{
                if ((jt1.getText()).equals("")){
                    JOptionPane.showMessageDialog(this,"有项目缺少数据!");
                }else if ((jt2.getText()).equals("")){
                    JOptionPane.showMessageDialog(this,"有项目输入数据!");
                }else if (jt3.getText().equals("")){
                    JOptionPane.showMessageDialog(this,"有项目缺少数据!");
                }else if ((jt4.getText()).equals("")||(Integer.parseInt(jt4.getText()))<0) {
                    JOptionPane.showMessageDialog(this,"有项目缺少数据或不符合要求!");
                }else{
                    Object[] objects = new Object[5];
                    objects[0] = cid;
                    objects[1] = jt1.getText();
                    objects[2] = jt2.getText();
                    objects[3] = jt3.getText();
                    objects[4] = Integer.parseInt(jt4.getText());
                    try {
                        DataBaseUtil.changeResultSet(sql2,objects);
                    } catch (SQLException ex) {
                        ex.printStackTrace();
                    }
                    cid = getNextCustomerEid();
                }
            }
        });

    }
    public String getFirstCustomerEid() throws SQLException {
        String d1 = null;
        rs = DataBaseUtil.produceResultSet(sql3);
        while(rs.next()){
            d1 = rs.getString(1);
        }
        char[] ar = "00000000".toCharArray();
        int n = Integer.parseInt(d1.replaceFirst("ct",""))+1;
        cid_int = n;
        int number =0;
        while(n>0){
            ar[7-number]= ((char)(n%10+48));
            n/=10;
            number++;
        }
        String n1 = new String(ar);
        return "ct"+n1;
    }
    public String getNextCustomerEid(){
        if(cid_int!=-1) {
            char[] ar = "00000000".toCharArray();
            cid_int++;
            int number = 0;
            int n = cid_int;
            while (n > 0) {
                ar[7 - number] = ((char) (n%10+48));
                n /= 10;
                number++;
            }
            String n1 = new String(ar);
            return "ct" + n1;
        }
        return null;
    }
    private class CustomLayout implements  LayoutManager{

        @Override
        public void addLayoutComponent(String name, Component comp) {

        }

        @Override
        public void removeLayoutComponent(Component comp) {

        }

        @Override
        public Dimension preferredLayoutSize(Container parent) {
            return null;
        }

        @Override
        public Dimension minimumLayoutSize(Container parent) {
            return null;
        }

        @Override
        public void layoutContainer(Container parent) {
            int width = getWidth();
            int height = getHeight();
            if(jc1.isVisible()){
                jc1.setBounds(width/8, height/8,60,30);
            }
            if(jt1.isVisible()){
                jt1.setBounds(width/8+60, height/8,100,25);
            }
            if(jc2.isVisible()){
                jc2.setBounds(width/8, height/8+25,60,25);
            }
            if(jt2.isVisible()){
                jt2.setBounds(width/8+60, height/8+25,100,25);
            }
            if(jc3.isVisible()){
                jc3.setBounds(width/8, height/8+50,60,25);
            }
            if(jt3.isVisible()) {
                jt3.setBounds(width / 8 + 60, height / 8 + 50, 100, 25);
            }
            if(jc4.isVisible()){
                jc4.setBounds(width/8,height/8+75,60,25);
            }
            if(jt4.isVisible()){
                jt4.setBounds(width/8+60, height/8+75,100,25);
            }
            if(jc5.isVisible()){
                jc5.setBounds(width/2,height/8,60,25);
            }
            if (jt5.isVisible()){
                jt5.setBounds(width/2+60,height/8,100,25);
            }
            if(jb1.isVisible()) {
                jb1.setBounds(7 * width / 8 - 70, height / 11, 70, height / 5);
            }
        }
    }
}